<?php
class BookCaseController extends Controller{
	//展示所有图书
	public function indexAction(){
		$title="书架管理";
		$include_file="allcase";
		$nav=$this->getNavs();
		$libraryinfo=$this->getFooter();
		$casemodel=new CaseModel("tb_bookcase");
		//$allbook=$bookmodel->getAllBook();
		//定义每页数量
		$pagesize = 10;
		//接收页数参数,如果为空,则显示第一页
		$current = isset ( $_GET ['page'] ) ? $_GET ['page'] : 1;
		//计算偏移量
		$offset = ($current - 1) * $pagesize;
		//获取总条数,条件where为空
		$total = $casemodel->total("");
		$bookcases = $casemodel->getCases($offset, $pagesize);
		
		//载入分页工具类,获取分页信息
		$this->library ( "Page" );
		$page=new Page($total, $pagesize, $current,"index.php",array("c"=>"bookcase","a"=>"index"));
		$pageinfo=$page->showPage();
		include VIEWS_PATH."index.html";
	}

	//载入添加图书表单
	public function addAction(){
		$title="新建书架";
		$include_file="addcase";
		$nav=$this->getNavs();
		$libraryinfo=$this->getFooter();
		include VIEWS_PATH."index.html";
	}

	//插入数据
	public function insertAction(){
		$data['name']=$_POST['name'];
		//判断是否为空
		if (empty($data['name'])) {
			$this->jump("index.php?c=bookcase&a=add","书架名称不能为空不能为空!",3);
		}
		//转义+实体化
		$this->helper("input");
		$data=deepspecialchars($data);
		$typemodel=new TypeModel("tb_bookcase");
		if ($typemodel->insert($data)) {
			$this->jump("index.php?c=bookcase&a=index","新建书架成功成功!",2);
		}else {
			$this->jump("index.php?c=bookcase&a=add","新建书架失败!",3);
		}
	}

	//修改图书表单
	public function editAction(){
		$title="修改书架名称";
		$include_file="editcase";
		$nav=$this->getNavs();
		$libraryinfo=$this->getFooter();
		//强制转为数字
		$pk=$_GET['id']+0;
		$casemodel=new CaseModel("tb_bookcase");
		$onecase=$casemodel->selectByPk($pk);
		include VIEWS_PATH."index.html";
	}

	//完成图书更新
	public function updateAction(){
		$data['name']=$_POST['name'];
		$data['id']=$_POST['id']+0;
		//判断是否为空
		if (empty($data['name'])) {
			$this->jump("index.php?c=bookcase&a=edit&id={$data['id']}","书架名称不能修改为空!",3);
		}
		//转义+实体化
		$this->helper("input");
		$data=deepspecialchars($data);
		$casemodel=new TypeModel("tb_bookcase");
		if ($casemodel->update($data)) {
			$this->jump("index.php?c=bookcase&a=index","修改成功!",2);
		}else {
			$this->jump("index.php?c=bookcase&a=edit&id={$data['id']}","修改失败!",3);
		}
	}

	//删除图书
	public function deleteAction(){
		$pk=$_GET['id']+0;
		$bookmodel=new BookModel("tb_bookinfo");
		$allbook=$bookmodel->total("bookcase={$pk}");
		
		if ($allbook!=0){
			$this->jump("index.php?c=bookcase&a=index","该书架上有图书,请移除该书架上的所有图书后再删除书架!",4);
		}
		$typemodel=new TypeModel("tb_bookcase");
		if ($typemodel->delete($pk)) {
			$this->jump("index.php?c=bookcase&a=index", "删除成功!",2);
		}else {
			$this->jump("index.php?c=bookcase&a=index", "删除失败,请刷新页面后重试!",3);
		}
	}
}